package com.ruoyi.web.controller.gallery;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.gallery.GalleryInfo;
import com.ruoyi.system.service.gallery.GalleryInfoService;
import com.ruoyi.system.utils.FileUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 图库Controller
 * 
 * @author 张少丛
 * @date 2020-11-30
 */
@RestController
@RequestMapping("/gallery/info")
public class GalleryInfoController extends BaseController
{
    @Autowired
    private GalleryInfoService galleryInfoService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询图库列表
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(GalleryInfo galleryInfo)
    {
        startPage();
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<GalleryInfo> list = galleryInfoService.selectGalleryInfoList(galleryInfo,userId);
        return getDataTable(list);
    }

    /**
     * 导出图库列表
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:export')")
    @Log(title = "图库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GalleryInfo galleryInfo)
    {
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        List<GalleryInfo> list = galleryInfoService.selectGalleryInfoList(galleryInfo,userId);
        ExcelUtil<GalleryInfo> util = new ExcelUtil<GalleryInfo>(GalleryInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 获取图库详细信息
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:query')")
    @GetMapping(value = "/{galleryInfoId}")
    public AjaxResult getInfo(@PathVariable("galleryInfoId") Long galleryInfoId)
    {
        return AjaxResult.success(galleryInfoService.selectGalleryInfoById(galleryInfoId));
    }

    /**
     * 新增图库
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:add')")
    @Log(title = "图库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GalleryInfo galleryInfo)
    {
        return toAjax(galleryInfoService.insertGalleryInfo(galleryInfo));
    }

    /**
     * 修改图库
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:edit')")
    @Log(title = "图库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GalleryInfo galleryInfo)
    {
        return toAjax(galleryInfoService.updateGalleryInfo(galleryInfo));
    }

    /**
     * 删除图库
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:remove')")
    @Log(title = "图库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{galleryInfoIds}")
    public AjaxResult remove(@PathVariable Long[] galleryInfoIds)
    {
        return toAjax(galleryInfoService.deleteGalleryInfoByIds(galleryInfoIds));
    }

    /**
     * 查询店铺列表
     * @return
     */
    @PreAuthorize("@ss.hasPermi('gallery:info:query')")
    @GetMapping(value = "/selectSupplierList")
    public AjaxResult selectSupplierList()
    {
        // 获取登录用户ID
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        Long userId = loginUser.getUser().getUserId();
        return AjaxResult.success(galleryInfoService.selectSupplierList(userId));
    }

    /**
     * 上传图片
     * @param file
     * @return
     */
    @PostMapping("/uploadImage")
    public Map<String,Object> up(MultipartFile file){
        String url = "/home/ruoyi/profile/xiazai";
        return FileUtil.uploadImage(file,url);
    }

}
